package com.yj.suncity.publish;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import com.yj.suncity.R;
import com.yj.suncity.async.AsyncTaskUtil;
import com.yj.suncity.async.AsyncTaskUtil.ResultCallBack;
import com.yj.suncity.constant.URLCon;
import com.yj.suncity.utis.TimePickUtils;
import com.yj.suncity.utis.ToastUtil;
import com.yj.suncity.utis.Utils;
import com.yj.suncity.utis.TimePickUtils.OnTimeDateSelectListener;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class PublishXuanshangActivity extends Activity implements
		OnClickListener {

	private Context context;
	private String userId;
	private TextView mTxt_end_time, mTxt_start_time;
	private EditText mEdt_offer_price, mEdt_offer_num, mEdt_offer_detail;
	private List<NameValuePair> pairs = new ArrayList<NameValuePair>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_publish_offer);
		context = this;
		userId = getIntent().getStringExtra("userid");
		findView();
	}

	private void findView() {
		findViewById(R.id.btnHeadBack).setOnClickListener(this);
		findViewById(R.id.btn_ok).setOnClickListener(this);
		mEdt_offer_price = (EditText) findViewById(R.id.edt_xuanshang_price);
		mEdt_offer_num = (EditText) findViewById(R.id.edt_xuanshang_numt);
		mEdt_offer_detail = (EditText) findViewById(R.id.edt_xuanshang_detail);

		mTxt_start_time = (TextView) findViewById(R.id.txt_start_time);
		mTxt_end_time = (TextView) findViewById(R.id.txt_end_time);
		mTxt_end_time.setOnClickListener(this);
		mTxt_start_time.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnHeadBack:
			this.finish();
			break;
		case R.id.txt_end_time:
			TimePickUtils.dateSelect(context, new OnTimeDateSelectListener() {
				@Override
				public void onDateListener(String time) {
					mTxt_end_time.setText(time);
				}
			});
			break;
		case R.id.txt_start_time:
			TimePickUtils.dateSelect(context, new OnTimeDateSelectListener() {
				@Override
				public void onDateListener(String time) {
					mTxt_start_time.setText(time);
				}
			});
			break;
		case R.id.btn_ok:
			String offerPrice = mEdt_offer_price.getText().toString().trim();
			String offerNum = mEdt_offer_num.getText().toString().trim();
			String offerDetail = mEdt_offer_detail.getText().toString().trim();
			String offerEndTime = mTxt_end_time.getText().toString().trim();
			String offerStartTime = mTxt_start_time.getText().toString().trim();
			offerStartTime = Utils.TimeToTimestamp(offerStartTime);
			offerEndTime = Utils.TimeToTimestamp(offerEndTime);

			if (offerPrice.equals("")) {
				ToastUtil.ShowToast(context, "请输入悬赏金额！");
				return;
			}
			
			if (Integer.valueOf(offerPrice)<=0) {
				ToastUtil.ShowToast(context, "悬赏金额必须大于0！");
				return;
			}
			
			if (offerNum.equals("")) {
				ToastUtil.ShowToast(context, "请输入承接人数！");
				return;
			}
			if (offerStartTime.equals("")) {
				ToastUtil.ShowToast(context, "请选择起始日期！");
				return;
			}
			if (offerEndTime.equals("")) {
				ToastUtil.ShowToast(context, "请选择截止日期！");

				return;
			}
			if (offerDetail.equals("")) {
				ToastUtil.ShowToast(context, "请输入悬赏内容！");
				return;
			}
			PublishOffer(offerPrice, offerNum, offerDetail, offerStartTime,
					offerEndTime);

			break;
		default:
			break;
		}
	}

	private void PublishOffer(String offerPrice, String offerNum,
			String offerDetail, String offerStartTime, String offerEndTime) {
		pairs.clear();
		pairs.add(new BasicNameValuePair("action","publishoffer"));
		pairs.add(new BasicNameValuePair("txtofferid",""));
		pairs.add(new BasicNameValuePair("txtuserid",userId));
		pairs.add(new BasicNameValuePair("txtcontent",offerDetail));
		pairs.add(new BasicNameValuePair("txtgivenum",offerPrice));
		pairs.add(new BasicNameValuePair("txtbegindt",offerStartTime));
		pairs.add(new BasicNameValuePair("txtenddt",offerEndTime));
		pairs.add(new BasicNameValuePair("txtnum",offerNum));
		
		
		String path = URLCon.悬赏;
		AsyncTaskUtil asyncTaskUtil = new AsyncTaskUtil(context, pairs, null,
				false, null, true, "正在发布......", new ResultCallBack() {
					@Override
					public void JSONResult(JSONObject jsonObject) {
						if (jsonObject.optString("success").equals("1")) {
							ToastUtil.ShowToast(context, "发布成功！");
							PublishXuanshangActivity.this.finish();
						}else{
							ToastUtil.ShowToast(context, ""+jsonObject.optString("msg"));
						}
					}
				});
		asyncTaskUtil.execute(path);
	}

}
